package com.hay.oss.ali;

import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * alioss配置
 * @author LiMouRen
 * @date 2023/7/26
 */
@Configuration
@EnableConfigurationProperties({AliOssProperties.class})
@ConditionalOnProperty(name = "ali-oss.enable",havingValue = "true")
public class AliOssConfiguration {

    @Bean
    public AliOssTemplate aliOssTemplate(AliOssProperties aliOssProperties) {
        ClientConfiguration conf = new ClientConfiguration();
        conf.setMaxConnections(1024);
        conf.setSocketTimeout(50000);
        conf.setConnectionTimeout(50000);
        conf.setConnectionRequestTimeout(1000);
        conf.setIdleConnectionTime(60000L);
        conf.setMaxErrorRetry(5);
        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(aliOssProperties.getAccessKey(), aliOssProperties.getSecretKey());
        OSSClient ossClient = new OSSClient(aliOssProperties.getEndpoint(), credentialsProvider, conf);
        return new AliOssTemplate(aliOssProperties,ossClient);
    }

}
